package cn.blog.controller.front;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.blog.controller.BaseServlet;
import cn.blog.entity.origin.Article;
import cn.blog.entity.origin.Commet;
import cn.blog.entity.origin.User;
import cn.blog.service.front.ArticleService;
import cn.blog.service.front.CompileHeadService;

@WebServlet("/front/article")
public class ArticleServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uname = (String) req.getSession().getAttribute("uname");
		
		
		req.setAttribute("uname", uname);

		ArticleService art = new ArticleService();
		if (uname != null && uname != "" && !uname.equals("登录")) {
			int userId = art.getuserId(uname);
			req.setAttribute("userId", userId);
		}

		String login = "登录";
		String loginUrl = "login";

		String reg = "注册";
		String regUrl = "register";
		if (uname != null && uname != "" && !uname.equals("登录")) {
			
			login = uname;
			loginUrl = "compile";
			reg = "退出";
			regUrl = "login";
		}
		req.setAttribute("login", login);
		req.setAttribute("reg", reg);
		req.setAttribute("loginUrl", loginUrl);
		req.setAttribute("regUrl", regUrl);

		// 获取文章编号
		String artIdStr = req.getParameter("articleId");
		req.setAttribute("articleId", artIdStr);
		int artId = Integer.parseInt(artIdStr);

		req.getSession().setAttribute("url", "/front/article?articleId=" + artIdStr + "#dingwei");

		// 根据文章编号获取文章信息
		Article article = art.art(artId);
		req.setAttribute("article", article);
		
		int read = article.getArtRead()+1;
		art.updateRead(read,artId);

		// 根据用户编号获取用户信息
		User user = art.getUser(article.getArtUserId());
		req.setAttribute("user", user);

		// 获取用户园龄
		Date time1 = new Date();// 系统时间
		Date time2 = user.getUserRegTime();// 注册时间
		/* 先转成毫秒并求差 */
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String d1 = sdf.format(time1);
		String d2 = sdf.format(time2);
		long days = 0;
		try {
			days = sdf.parse(d1).getTime() - sdf.parse(d2).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long d = days / (1000 * 60 * 60 * 24);
		String day = (d > 30) ? d / 30 + "月" : d + "天";
		req.setAttribute("days", day);

		// 根据文章编号获取评论
		List<Commet> com = art.comAll(artId);
		req.setAttribute("com", com);

		forward("front/article");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		ArticleService art = new ArticleService();
		String artId = req.getParameter("articleId");
		int articleId = Integer.parseInt(artId);
		String comPeoName = (String) req.getSession().getAttribute("uname");// 获取评论者姓名
		int userId = art.getuserId(comPeoName);// 获取评论者编号

		// 获取系统当前时间，并转换为datetime类型
		Date time = new Date();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String t = sdf.format(time);
		java.util.Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(t);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		java.sql.Date comTime = new java.sql.Date(date.getTime());
		
		String comText = req.getParameter("comText");
		
		art.addCom(articleId,userId,comPeoName,comText,comTime);

		redirect("/front/article?articleId=" + artId + "#dingwei");
	}

}
